package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.exception.LeaseException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.LeaseStatus;
import com.atguigu.lease.web.admin.mapper.LeaseAgreementMapper;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.agreement.AgreementQueryVo;
import com.atguigu.lease.web.admin.vo.agreement.AgreementVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author liubo
 * @description 针对表【lease_agreement(租约信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class LeaseAgreementServiceImpl extends ServiceImpl<LeaseAgreementMapper, LeaseAgreement>
        implements LeaseAgreementService {
    @Autowired
    private ApartmentInfoService apartmentInfoService;
    @Autowired
    private RoomInfoService roomInfoService;
    @Autowired
    private LeaseTermService leaseTermService;
    @Autowired
    private PaymentTypeService paymentTypeService;
    @Autowired
    private LeaseAgreementMapper leaseAgreementMapper;
    @Override
    public IPage<AgreementVo> findAgreementVoPage(IPage<AgreementVo> page, AgreementQueryVo queryVo) {
        return leaseAgreementMapper.findAgreementVoPage(page,queryVo);
    }

    @Override
    public AgreementVo findAgreementVoById(Long id) {
        //根据id查询租约信息
        LeaseAgreement leaseAgreement = this.getById(id);
        //查询租约所对应的公寓信息
        ApartmentInfo apartmentInfo = apartmentInfoService.getById(leaseAgreement.getApartmentId());
        //查询租约所对应的房间信息
        RoomInfo roomInfo = roomInfoService.getById(leaseAgreement.getRoomId());

        //查询租约所对应的租期信息
        LeaseTerm leaseTerm = leaseTermService.getById(leaseAgreement.getLeaseTermId());
        //查询租约所对应的支付方式信息
        PaymentType paymentType = paymentTypeService.getById(leaseAgreement.getPaymentTypeId());
        //创建AgreementVo对象
        AgreementVo agreementVo = new AgreementVo();
        //复制属性
        BeanUtils.copyProperties(leaseAgreement,agreementVo);
        agreementVo.setApartmentInfo(apartmentInfo);
        agreementVo.setRoomInfo(roomInfo);
        agreementVo.setLeaseTerm(leaseTerm);
        agreementVo.setPaymentType(paymentType);

        return agreementVo;
    }

    @Override
    public void removeLeaseAgreementById(Long id) {
        //根据id查询租约信息
        LeaseAgreement leaseAgreement = this.getById(id);
        //判断当前租约信息的状态是不是2或5
        if(leaseAgreement.getStatus().equals(LeaseStatus.SIGNED) ||
        leaseAgreement.getStatus().equals(LeaseStatus.WITHDRAWING)){
            //若当前租约的状态为2或5，则不允许删除
            throw new LeaseException(ResultCodeEnum.DELETE_LEASE_ERROR);
        }
        //若当前的租约信息不为2或5,则直接删除
        this.removeById(id);
    }
}




